package study.java.thread10;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
* @Name: DeleteDirTimerDemo
* @Description: 定时器测试：指定时间删除指定的目录
* @Author: 李超	lc_jack@foxmail.com
* @CreateDate: 2017-1-10 上午10:27:55 
* @Version: V1.0
 */
public class DeleteDirTimerDemo {
	
	public static void main(String[] args) throws ParseException {
		//创建定时器
		Timer timer = new Timer("删除指定目录");
		//创建定时点
		String s = "2017-01-10 10:52:30" ;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ; 
		Date time = sdf.parse(s) ;
		//执行定时任务
		String dirPath = "D:\\dirTest" ;
		timer.schedule(new DeleteDirTask(dirPath), time) ;
	}

}

/**
* @Name: DeleteDirTask
* @Description: 删除指定目录任务
* @Author: 李超	lc_jack@foxmail.com
* @CreateDate: 2017-1-10 上午10:31:02 
* @Version: V1.0
 */
class DeleteDirTask extends TimerTask {
	
	private String dirPath ;
	
	public DeleteDirTask(String dirPath) {
		this.dirPath = dirPath;
	}

	@Override
	public void run() {
		File dir = new File(dirPath) ;
		deleteDir(dir) ;
	}

	/**
	* @Name: deleteDir
	* @Description: 递归删除目录
	* @Author: 李超	lc_jack@foxmail.com
	* @Version: V1.0
	* @CreateDate: 2017-1-10 上午10:32:39
	* @Parameters: @param dir
	* @Return: void
	 */
	private void deleteDir(File dir) {
		File[] fileArray = dir.listFiles() ;
		if(fileArray != null) {
			for (File file : fileArray) {
				if(file.isDirectory()) {
					deleteDir(file) ;
				} else {
					System.out.println(file.getName() + ":" + file.delete());
				}
			}
			System.out.println(dir.getName() + ":" + dir.delete());
		}
	}
}